﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ongoal.Quotation
{
    public static class CollectionExtensions
    {
        public static T Find<T>(this T[] items, Predicate<T> predicate)
        {
            return Array.Find(items, predicate);
        }

        public static T[] FindAll<T>(this T[] items, Predicate<T> predicate)
        {
            return Array.FindAll(items, predicate);
        }

        //
        // 摘要:
        //     Checks whether or not collection is null or empty. Assumes collection can be
        //     safely enumerated multiple times.
        public static bool IsNullOrEmpty(this IEnumerable @this)
        {
            if (@this != null)
            {
                return !@this.GetEnumerator().MoveNext();
            }

            return true;
        }

        //
        // 摘要:
        //     Generates a HashCode for the contents for the list. Order of items does not matter.
        //
        //
        // 参数:
        //   list:
        //     The list.
        //
        // 类型参数:
        //   T:
        //     The type of object contained within the list.
        //
        // 返回结果:
        //     The generated HashCode.
        public static int GetContentsHashCode<T>(IList<T> list)
        {
            if (list == null)
            {
                return 0;
            }

            int num = 0;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i] != null)
                {
                    num += list[i].GetHashCode();
                }
            }

            return num;
        }

        //
        // 摘要:
        //     Determines if two lists are equivalent. Equivalent lists have the same number
        //     of items and each item is found within the other regardless of respective position
        //     within each.
        //
        // 参数:
        //   listA:
        //     The first list.
        //
        //   listB:
        //     The second list.
        //
        // 类型参数:
        //   T:
        //     The type of object contained within the list.
        //
        // 返回结果:
        //     True if the two lists are equivalent.
        public static bool AreEquivalent<T>(IList<T> listA, IList<T> listB)
        {
            if (listA == null && listB == null)
            {
                return true;
            }

            if (listA == null || listB == null)
            {
                return false;
            }

            if (listA.Count != listB.Count)
            {
                return false;
            }

            List<T> list = listB.ToList();
            for (int i = 0; i < listA.Count; i++)
            {
                bool flag = false;
                for (int j = 0; j < list.Count; j++)
                {
                    if (object.Equals(listA[i], list[j]))
                    {
                        flag = true;
                        list.RemoveAt(j);
                        break;
                    }
                }

                if (!flag)
                {
                    return false;
                }
            }

            return true;
        }
    }
}
